const fs = require("fs");
const userService = require("../service/user.service");
const { UPLOAD_PATH } = require("../config/path");
const BaseController = require("./BaseController");
class UserController extends BaseController {
  async create(ctx, next) {
    ctx.body = "user register";
    //1.获取用户传递过来的信息
    //3.将user信息存储到数据库中
    const user = ctx.request.body;
    console.log("创建开始", user);
    const [result] = await userService.create(user);
    console.log("创建结束");
    //4.查看存储的结果,告知前端创建成功
    ctx.body = {
      code: 200,
      data: result,
      msg: "创建成功",
    };
  }
  async showAvatarImage(ctx, next) {
    //1.获取用户的ID
    const { userId } = ctx.params;
    //2.根据用户的ID查询用户的头像信息
    const avatarInfo = await userService.queryAvatarWithUserId(userId);
    //3.读取头像所在的文件
    const { filename, mimetype } = avatarInfo;
    console.log(`${mimetype}`);

    ctx.type = mimetype;
    ctx.body = fs.createReadStream(`${UPLOAD_PATH}/${filename}`);
  }
}

module.exports = new UserController(userService);
